<?php


namespace app\common\model;


use think\Db;
use think\Exception;

class ZhunleCounselorProblemRecode extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.zhunle_counselor_problem_recode';

    /**
     * 档案备份
     * @param array $rids
     * @return array
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function copeRecode(array $rids)
    {
        $recodeDataModels = Recode::whereIn('id', $rids)
            ->field('uid,aid,isself,name,type,year,month,day,hour,minute,timezone,birth_country,birth_province,birth_city,live_country,live_province,live_city,comments,long_deg,long_min,ew,lat_deg,lat_min,ns,is_summer,avatar,from,or_day')
            ->select();
        //判断档案信息是否有效
        if (count($rids) !== count($recodeDataModels))
        {
            throw new Exception('档案数据异常', 0);
        }

        try
        {
            $recodeData = [];
            foreach ($recodeDataModels as $recodeDataModelItem)
            {
                array_push($recodeData, $recodeDataModelItem->data);
            }

            $resultModels = $this->saveAll($recodeData);
        }
        catch (Exception $e)
        {
            throw new Exception('服务异常', 0);
        }

        $recodeIds = [];
        foreach ($resultModels as $recodeModeItem)
        {
            array_push($recodeIds, $recodeModeItem->id);
        }

        return $recodeIds;
    }

    /**
     * 获取问题确认档案信息
     * @param $recodeIds
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCreateProblemRecode($recodeIds): array
    {
        $recodeData = $this->whereIn('id', $recodeIds)
            ->field('id,name,year,month,day,hour,minute,timezone,birth_country,birth_province,birth_city,live_country,live_province,live_city')
            ->select();

        $recodeInfo = [];
        foreach ($recodeData as $recodeItem)
        {
            // 出生地址
            $birthAddress = $recodeItem->birth_country . ' ' . $recodeItem->birth_province . ' ' . $recodeItem->birth_city;
            // 现居地址
            $liveAddress = $recodeItem->live_country . ' ' . $recodeItem->live_province . ' ' . $recodeItem->live_city;
            // 出生日期
            $birthTime = $recodeItem->year . '-' . sprintf("%02d", $recodeItem->month) . '-' . sprintf("%02d", $recodeItem->day) . ' ' . sprintf("%02d", $recodeItem->hour) . ':' . sprintf("%02d", $recodeItem->minute);

            $recodeInfo[] = [
                'name' => $recodeItem->data['name'],
                'birth_address' => $birthAddress,
                'live_address' => $liveAddress,
                'rid' => $recodeItem->id,
                'birth_time' => $birthTime,
            ];
        }

        return $recodeInfo;
    }

}
